<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../../../layuiadmin/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../../layuiadmin/style/admin.css" media="all">
		<link rel="stylesheet" href="../../../../css/common.css" media="all">
		<style type="text/css">
			.show_img_ul {
				display: flex;
				flex-wrap: wrap;
				align-items: center;
				margin-top: 10px;
			}
			
			.show_img_li {
				text-align: center;
				padding: 10px;
			}
			
			.show_img_li p,
			.show_img_li1 p {
				text-align: center;
				cursor: pointer;
			}
			
			.show_img_li img {
				width: 120px;
				height: 120px;
				margin-bottom: 8px;
			}
			
			.show_img_li1 img {
				width: 300px;
				height: 200px;
				margin-bottom: 8px;
			}
			
			.layui-upload-img {
				width: 92px;
				height: 92px;
				margin: 0 10px 10px 0;
			} 
			.layui-upload-img { width: 90px; height: 90px; margin: 0; }
			.layui-elem-quote  { width:100%; left; margin: 10px 0px 0px 0px;}
			.layui-elem-quote  li { width:90px; display: inline-block; margin-right: 5px;}
			.layui-elem-quote  li .layui-input { display: initial; }
			.layui-elem-quote  li a { position: absolute; top: 0; display: block; }
			.layui-elem-quote  li a i { font-size: 24px; background-color: #008800; }   
			#lunbo .item_img img{ width: 90px; height: 90px;}
			#lunbo li{position: relative;margin: 10px.;}
			#lunbo li .operate{ color: #000; display: none;}
			#lunbo li .toleft{ position: absolute;top: 40px; left: 1px; cursor:pointer;}
			#lunbo li .toright{ position: absolute;top: 40px; right: 1px;cursor:pointer;}
			#lunbo li .close{position: absolute;top: -8px; right: -8px;cursor:pointer;}
			#lunbo li:hover .operate{ display: block;}
		</style>
	</head>

	<body>

		<div class="layui-fluid">
			<div class="layui-row" id="view">
				<form class="layui-form" lay-filter="test_detail">
					<div class="layui-card">
						<div class="layui-card-body">
							
							<div class="layui-form-item">
								<label class="layui-form-label">课程类型 <span style="color:red">*</span></label>
								<div class="layui-input-inline">
									<input type="text" name="catname" lay-verify="required" placeholder="请选择课程" autocomplete="off" class="layui-input catname">
									<input type="hidden" name="catid" lay-verify="required" placeholder="请选择课程" autocomplete="off" class="layui-input catid">
								</div>
								<div class="layui-form-mid layui-word-aux">
									<a href="Javascript:;" onclick="searchdata()">选择</a>
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">视频状态</label>
								<div class="layui-input-block on_off"></div>
							</div>
							<!--商品公告信息-->
							<!--<div class="layui-form-item">
								<div class="layui-inline">
									<label class="layui-form-label">播放量</label>
									<div class="layui-input-inline">
										<input type="text" name="video_views" placeholder="请输入播放量" autocomplete="off" class="layui-input">
									</div>
								</div>
									<label class="layui-form-label">是否推荐</label>
									<div class="layui-input-inline">
										<input type="radio" name="recommend" value="1" title="是" >
										<input type="radio" name="recommend" value="0" title="否" checked="">
									</div>
							</div>			-->
							<div class="layui-form-item">
								<label class="layui-form-label">视频标题</label>
								<div class="layui-input-block">
									<input type="text" name="title" placeholder="请输入课程标题"  lay-verify="required"  autocomplete="off" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">排序</label>
								<div class="layui-input-block">
									<input type="text" name="oid" placeholder="排序" autocomplete="off" class="layui-input" >
								</div>
							</div>
							<!--<div class="layui-form-item">
								<label class="layui-form-label">课程内容简介</label>
								<div class="layui-input-block">
									<textarea placeholder="请输入课程内容简介" name="content" class="layui-textarea"></textarea>
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">课程图片</label>
								<div class="layui-input-block">
									<button type="button" class="layui-btn layui-btn-normal" onclick="choose(0)">上传图片</button>
									<ul class="show_img_ul" id="img"></ul>
								</div>
							</div>-->
							<div class="layui-form-item upvideo">
								<label class="layui-form-label">课程视频</label>
								<div class="layui-input-block">
									<button type="button" class="layui-btn layui-btn-normal upimgbtn" onclick="choose(3)">上传视频</button>
									<blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
										预览视频：
										<div class="layui-upload-list upload_list" id="video">
											<ul class="show_img_ul" id="video"></ul>
										</div>
									</blockquote>

								</div>
							</div>
						</div>
					</div>

					<div class="layui-form-item layui-layout-admin">
						<div class="layui-input-block">
							<div class="layui-footer" style="left: 0;">
								<!-- <button type="reset" class="layui-btn layui-btn-primary">重置</button> -->
								<button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>
		<script src="../../../../layuiadmin/layui/layui.js"></script>
		<script src="../../../../js/jquery.min.js"></script>
		<script src="../../../../js/jquery.jqprint-0.3.js"></script>
		<script src="../../../../js/common.js"></script>
		<script>
			var _data = {
				id: getParam('id')
			}
			function child(data, index) {
				$(" input[name=catid]").val(data.cat_id);
				$(" input[name=catname]").val(data.catname);
				console.log(data)
			}
			function child_shop(data, index) {
				$(" input[name=shopid]").val(data.cat_id);
				console.log(data)
			}

			layui.config({
				base: '../../../../layuiadmin/' //静态资源所在路径
			}).extend({
				tinymce: '../tinymce/tinymce',
				index: 'lib/index' //主入口模块
			}).use(['index', 'form', 'laytpl', 'laydate', 'layedit', 'upload','tinymce'], function() {
				var $ = layui.$,
					admin = layui.admin,
					upload = layui.upload,
					laytpl = layui.laytpl,
					laydate = layui.laydate,
					form = layui.form,
					tinymce = layui.tinymce,
					layedit = layui.layedit;

				var img_arr = [],
					img_arr1 = [];
					
				if(_data.id){
					initdata();
				}
				
				on_off_show('radio');
//				layedit.build('demo_editor'); //建立编辑器
				var edit = tinymce.render({
					elem: "#demo_editor",
					height: 600
				});

				//监听提交
				form.on('submit(demo1)', function(data) {
					var data = data.field;
					_data.img = img_arr.join(',');
					_data.video = img_arr1.join(',');
					_data = Object.assign(_data, data); 
//					_data.content = edit.getContent();
					console.log(data);
					for (item in data){ 
						if(/^c/.test(item)){
							if(item.substr(0,6) == 'client'){ 
								_data.client += data[item]+','
								console.log(item)
								console.log(data[item])
							} 
						} 
					} 
					layer.confirm('确定要提交数据？', function() {
						loading();
						if(_data.id) {
							submitdata(server.class_edit);
						} else {
							delete _data.id
							submitdata(server.class_insert);
						}
					}, function() {
						layer.msg('取消操作');
					});
					return false;
				});

				function submitdata(_url) {
					$.post(_url, _data, function(res, status) {
						if(status == 'success') {
							if(res.code == 0) {
								layer.alert('操作成功', function() {
									parent.layer.closeAll();
									parent.layui.table.reload('test-table-operate');
								});
							} else {
								layer.msg(res.msg);
							}
							closeloading();
						} else {
							closeloading();
							layer.msg('服务器连接错误');
						}
					});
				}

				window.choose = function(data) {
					layer.open({
						type: 2,
						title: '上传图片',
						shadeClose: false,
						shade: 0.5,
						maxmin: true, //开启最大化最小化按钮
						area: ['700px', '400px'],
						content: 'up_report.html?type=' + data,
						end: function() {},
						success: function() {}
					});
				}

				window.getimg = function(data, type) {
					console.log(data);
					if(data) {
						switch(type) {
							case '0':
								createli(data, 'img', type)
								break;
							case '3':
								createli(data, 'video', type)
								break;
							default:
								break;
						}
					}
				}
				window.delimg = function(i, type, _self) {
					console.log(type)
					var _type = String(type)
					layer.confirm('确定要删除图片？', function(index) {
						switch(_type) {
							case '0':
								img_arr.splice(i, 1)
								break;
							case '3':
								img_arr1.splice(i, 1)
								break;
							default:
								break;
						}
						$(_self).parent().remove();
						layer.close(index)
					}, function() {
						layer.msg('取消操作');
					});
					// console.log(i);
					// console.log(type);

				}

				function createli(data, dom, type) {
					var lihtml = '';
					switch(type) {
						case '0':
							if(data.indexOf(',') == -1) {
								img_arr.push(data)
							} else {
								for(var i = 0; i < data.split(',').length; i++) {
									img_arr.push(data.split(',')[i])
								}
							}
							for(var i = 0; i < img_arr.length; i++) {
								lihtml += '<li class="show_img_li">' +
									'<img src=' + img_arr[i] + ' >' +
									'<p onclick="delimg(' + i + ',0,this)">删除</p>' +
									'</li>'
							}
							break;
						case '3':
							if (data.indexOf(',') == -1) {
								img_arr1.push(data)
							} else {
								for (var i = 0; i < data.split(',').length; i++) {
									img_arr1.push(data.split(',')[i])
								}
							}
							for (var i = 0; i < img_arr1.length; i++) {
								lihtml += '<li class="show_img_li">' +
									'<video controls="controls" id="video{{index}}" width="320" height="240"><source src=' + img_arr1[i] + ' type="video/ogg" /><source src=' + img_arr1[i] + ' video/mp4" /></video>' +
									'<p onclick="delimg(' + i + ',3,this)">删除</p>' +
									'</li>'
							}
							
							break;
						default:
							break;
					}
					$('#' + dom).html(lihtml);
				}
				window.searchdata = function(type) {
					layer.open({
						type: 2,
						title: '选择',
						shadeClose: false,
						shade: 0.5,
						maxmin: true, //开启最大化最小化按钮
						area: ['80%', '80%'],
						content: 'choose_cat.html',
						end: function() {},
						success: function() {}
					});
				}

				function initdata() {
					$.get(server.class_detail, _data, function(res, status) {
						if(status == 'success') {
							if(res.code == 0) {
								var data = res.class
//								if($("select[name=catname] option").length > 0) {
//									$("select[name=catid]").val(data.data.catid);
//								}
								data.data.img ? createli(data.data.img, 'img', '0') : '',
								data.data.video ? createli(data.data.video, 'video', '3') : '',	
								_data.catname = data.data.catname;
								console.log(data.data);
								form.val("test_detail", data.data);
								form.render();
								form.render('select');
								closeloading()
								
								onload_img();
//								edit.setContent(data.data.content);
							} else {
								layer.msg(res.msg);
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}
				 // 图片拖动排序 拖动变换元素顺序
		    	function onload_img(){
		    		var moveItem=document.getElementsByClassName('item_img');
		    		let  pre_index;
		    		let  end_index;
		    		var draggingElementPosition = null;
				    var dragElement=null;
				    for(let i=0;i<moveItem.length;i++){
				    	 for (let a = 0; a < moveItem.length; a++) {
						     moveItem[a].setAttribute("draggable", true);
						 }
				    	
				    	
				        moveItem[i].addEventListener('dragstart',function (evt) {
				             dragElement=this;
				             //dragElement = evt.targe 
				             pre_index=$('.item_img').index($(this)) //移动前的下标 
				        },false);
				        
				        moveItem[i].addEventListener('drop',function (evt) { 
				            if(dragElement != this ){
				                //this.parentNode.insertBefore(dragElement,this); //插入  
				                 const draggingElementOrder = Array.from(this.parentElement.children).indexOf(dragElement);
				                 const order = Array.from(this.parentElement.children).indexOf(this);
				                 draggingElementPosition = dragElement.getBoundingClientRect();
								 //从大的序号移入到小的序号
								 if (draggingElementOrder > order) {
								   this.parentElement.insertBefore(dragElement, this);
								 }
								 //从小的序号移入到大的序号
								 else {
								   //节点不是最后一个
								   if (this.nextElementSibling) {
								     this.parentElement.insertBefore(dragElement, this.nextElementSibling);
								   }
								   // 节点是最后一个了，不能再用insertBefore
								   else {
								     this.parentElement.appendChild(dragElement);
								   }
								 }  
								 end_index=$('.item_img').index($(dragElement)) //移动后的下标  
				            }
				          
				        },false);
				        moveItem[i].addEventListener('dragleave',function (evt) {
				            var lists=$('.item_img');
				            for(var j=0;j<lists.length;j++){
				                lists.eq(j).j=j; 
				                var list=lists.eq(j).prevAll().length;  
				            }
				            if(dragElement != this){
				                if(this == this.parentNode.lastElementChild || this == this.parentNode.lastChild){
				                    this.parentNode.appendChild(dragElement);
				                }
				            }
				        },false)
				    };
				    document.ondragover = function(e){e.preventDefault();}
				    document.ondrop = function(e){ //释放鼠标后
				    	var s = img_arr_sort[pre_index]
			        	img_arr_sort.splice(pre_index,1)
			        	img_arr_sort.splice(end_index,0,s) ;//在指定的元素后插入
			        	console.log(img_arr_sort)
				    } 
				} 
	   			
				
			});
			//点击多图上传的X,删除当前的图片   
			$(document).on("click",".close",function(){
				var li_index=$(this).closest("li").index();
			    $(this).closest("li").remove();
			    img_arr_sort.splice(li_index,1) 
			  });
			 //多图上传点击<>左右移动图片
		    $(document).on("click",".layui-elem-quote li .toleft",function(){
		    	console.log("left")
		    	//#lunbo > li:nth-child(2) > div > i.toleft.layui-icon
		      var li_index=$(this).closest("li").index();
		       if(li_index>=1){
		        	$(this).closest("li").insertBefore($(this).closest("ul").find("li").eq(Number(li_index)-1));
		        	var s = img_arr_sort[li_index]
		        	img_arr_sort.splice(li_index,1)
		        	img_arr_sort.splice((Number(li_index)-1),0,s) //在指定的元素后插入
		       }
		    });
		    $(document).on("click",".layui-elem-quote li .toright",function(){
		      	var li_index=$(this).closest("li").index();
		      	$(this).closest("li").insertAfter($(this).closest("ul").find("li").eq(Number(li_index)+1));
		      	var s = img_arr_sort[li_index]
        	 	img_arr_sort.splice(li_index,1)
        		img_arr_sort.splice((Number(li_index)+1),0,s) 
		    });  
		</script>
	</body>

</html>